package rkw.database;

import org.springframework.beans.factory.annotation.Autowired;

import rkw.database.repositories.ElectionRepository;
import rkw.database.repositories.ElectionVoterRepository;
import rkw.database.repositories.TokenRepository;
import rkw.database.repositories.VoteRepository;
import rkw.database.repositories.VoterRepository;
import core.database.Node;
import core.database.model.NodeExtended;
import core.database.repositories.AdministratorTokenRepository;
import core.database.repositories.MemberRepository;
import core.database.repositories.NodeExtendedRepository;

public class DatabaseManager extends core.managers.DatabaseManager<NodeExtended, NodeExtendedRepository> {
	@Autowired
	AdministratorTokenRepository administratorTokenRepository;
	@Autowired
	MemberRepository memberRepository;
	@Autowired
	ElectionRepository electionRepository;
	@Autowired
	ElectionVoterRepository electionVoterRepository;
	@Autowired
	TokenRepository tokenRepository;
	@Autowired
	VoteRepository voteRepository;
	@Autowired
	VoterRepository voterRepository;

	@Override
	protected NodeExtended getNewEmptyNode() {
		return new NodeExtended();
	}

	@Override
	public void savePrefferedParentURI(String prefferedParentURI) {
		NodeExtended node = getMainNode();
		if (node != null) {
			node.setPrefferedParentURI(prefferedParentURI);
			nodeRepository.save(node);
		}
	}

	@Override
	public NodeExtended convertToDatabaseNode(Node node) {
		NodeExtended dbNode = new NodeExtended();
		dbNode.setAddress(node.getAddress());
		dbNode.setIp(node.getIp());
		dbNode.setLevel(node.getLevel());
		dbNode.setName(node.getName());
		dbNode.setNodeType(node.getNodeType());
		dbNode.setPort(node.getPort());
		return dbNode;
	}
}
